<?php
/**
 * @title 1208. 尽可能使字符串相等
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $s
     * @param String $t
     * @param Integer $maxCost
     * @return Integer
     */
    function equalSubstring($s, $t, $maxCost) {
        /**
         * @since 2021-02-05 长度
         */
        $len = strlen($s);

        /**
         * @since 2021-02-05 差值
         */
        $nums = [];
        for ($i=0; $i<$len; $i++){
            $nums[] = abs(ord($s[$i]) - ord($t[$i]));
        }
        // echo "nums=[", implode(", ", $nums) ,"]", PHP_EOL;

        /**
         * @since 2021-02-05 计算最大长度
         */
        $cost = 0;
        $maxlen = 0;
        for($p1=0; $p1<$len; $p1++){
            /**
             * @since 2021-02-05 至少能覆盖1个长度的成本
             */
            if($nums[$p1] <= $maxCost){
                $cost = $nums[$p1];
                $maxlen = 1;

                for($p2=$p1+1; $p2<$len; $p2++){
                    if($cost+$nums[$p2] <= $maxCost){
                        $maxlen++;
                        $cost += $nums[$p2];
                    } else {
                        break;
                    }
                }

                break;
            } else {}
        }

        /**
         * @since 2021-02-05 如果还是长度还是 0, 则直接跳出, p2未赋值
         */
        if($maxlen == 0){
            return $maxlen;
        } else {
            // echo "maxlen = {$maxlen}, nums[", $p1 ,"] = ", $nums[$p1] ,", nums[", $p2-1 ,"] = ", $nums[$p2-1] ,", cost={$cost}", PHP_EOL, PHP_EOL;
        }

        /**
         * @since 2021-02-05 移动指针
         */
        for (; $p2<$len; $p1++, $p2++){
            $cost = $cost - $nums[$p1] + $nums[$p2];
            // echo "maxlen = {$maxlen}, nums[", $p1+1 ,"] = ", $nums[$p1+1] ,", nums[{$p2}] = ", $nums[$p2] ,", cost={$cost}", PHP_EOL;

            /**
             * @since 2021-02-05 尝试增加长度
             */
            while($p2+1 < $len){
                if($cost+$nums[$p2+1] <= $maxCost){
                    $cost += $nums[$p2+1];
                    $maxlen++;
                    $p2++;
                    // echo "__ nums[{$p2}] = ", $nums[$p2] ,", cost = {$cost} maxlen={$maxlen}", PHP_EOL;
                } else {
                    break;
                }
            }
        }

        /**
         * @return
         */
        return $maxlen;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202102/2021.02.05.php
 */
$datas = [
    ["abcd", "bcdf", 3], // 3
    ["abcd", "cdef", 3], // 1
    ["abcd", "acde", 0], // 1
    ["krrgw", "zjxss", 19], // 2
    ["anryddgaqpjdw", "zjhotgdlmadcf", 5], // 1
    ["krpgjbjjznpzdfy", "nxargkbydxmsgby", 14], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->equalSubstring(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
